package wangjg.coco.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

public class HttpRequest {
	private String url;
	private HttpURLConnection conn=null;
	
	public HttpRequest(String url){
		this.url = url;
	}
	
	public String load(){
		try {
			return this.getContent();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
				conn.disconnect();
		}
		return null;
	}
	
	public String getContent() throws IOException{
		this.send(this.url);
		String charset = this.getCharset(conn.getContentType());
		String contentEncoding = conn.getContentEncoding();
		int maxlen=conn.getContentLength();
	
		InputStream bis = (InputStream) conn.getContent();
		ByteArrayOutputStream os=new ByteArrayOutputStream();
		transmit(bis, os, maxlen);
		byte[] body = os.toByteArray();
		
		if("gzip".equals(contentEncoding)){
			body = this.getUnzipData(body);
		}
		return new String(body,charset);
	}
	
	private String getCharset(String contentType){
		String defaultCharset = "iso-8859-1";
		if(contentType==null || contentType.length()==0){
			return defaultCharset;
		}
		String charset = "charset=";
		int pos = contentType.indexOf(charset);
		if(pos==-1){
			return defaultCharset;
		}
		return contentType.substring(pos + charset.length());
	}
	
	private byte[] getUnzipData(byte[] data) throws IOException{
		GZIPInputStream gzin = new GZIPInputStream(new ByteArrayInputStream(data));
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		transmit(gzin, os, -1);
		return os.toByteArray();
	}
	
	private void send(String sUrl) throws IOException{
		URL url = new URL(sUrl);
		conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(30000);
		conn.setReadTimeout(60000);
		conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
		conn.setRequestProperty("Host","translate.google.com.hk");
		conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		conn.setRequestProperty("Accept-Language","en-us,en;q=0.5");
		conn.setRequestProperty("Accept-Encoding","gzip, deflate");
		conn.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		conn.setRequestProperty("Connection","keep-alive");
		conn.setRequestProperty("Referer","http://translate.google.com.hk/");
		conn.setRequestProperty("Cookie","");
		conn.connect();
	}
	
	private void transmit(InputStream is,OutputStream os,int maxlen) throws IOException{
		byte[] buf = new byte[512];
		int count = 0;
		int total = 0;
		while((count = is.read(buf))!=-1){
			os.write(buf,0,count); 
			total += count;
			if(maxlen>0 && total>=maxlen){
				break;
			}
		}
	}

	public static void main(String[] args) throws IOException {
		HttpRequest req = new HttpRequest("http://news.163.com/");
		System.out.println(req.getContent());
	}
}
